import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/quant_warning_line_odoo_api.dart';
import 'package:flutter_admin/models/odoo/quant_warning_line.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/pages/odoo/product_productunitprice/product_list.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class QuantWarningLineEdit extends StatefulWidget {
  QuantWarningLineEdit({this.quantWarningLine});

  final QuantWarningLine? quantWarningLine;

  @override
  _QuantWarningLineEditState createState() => _QuantWarningLineEditState();
}

class _QuantWarningLineEditState extends State<QuantWarningLineEdit> {
  late QuantWarningLine quantWarningLine;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  List<SelectOptionVO> locationList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.quantWarningLine == null;
    this.quantWarningLine = widget.quantWarningLine ?? QuantWarningLine();
    _loadSelectVO();
  }

   _loadSelectVO() async{
     UserInfo userInfo = StoreUtil.getCurrentUserInfo();
     if (userInfo.major != null) {
       locationList = await DynamicDataApi.getModelSelect(
           active: false,
           model: "stock.location",
           name: "complete_name",
           key: "comment",
           val: 'XBK');
     } else {
       locationList = await DynamicDataApi.getModelSelect(
           active: false,
           model: "stock.location",
           name: "complete_name",
           key: "comment",
           val: 'XBK',
           key2: 'workshop_id',
           val2: int.parse(userInfo.deptId!));
     }
     setState(() {});
    setState(() { });
  }

  selectProduct() async {
    showDialog(
      context: context,
      builder: (BuildContext context) => Dialog(
        child: ProductList(),
      ),
    ).then((v) {
      if (v != null) {
        //_query();
        formKey.currentState!.save();
        quantWarningLine.productTmplId = v['id'].toString();
        quantWarningLine.productName = v['name'];
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '库房',
            dataList: locationList,
            value: quantWarningLine.locationId,
            onSaved: (v) {
              quantWarningLine.locationId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: '选择产品',
            value: quantWarningLine.productName,
            onSaved: (v) {
              quantWarningLine.productName = v;
            },
            onTap: () => selectProduct(),
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: '安全数量',
            value: quantWarningLine.quantity,
            onSaved: (v) {
              quantWarningLine.quantity = v;
            },
            validator: (v) {
              print(v);
              return !isBlank(v)?double.parse(v!) == 0 ? "安全数量不能为0" : null:"必填";
            },
          ),

        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            QuantWarningLineOdooApi.saveOrUpdate(quantWarningLine.toOdooMap()).then((ResponseBodyApi res) {
              if (!res.success!) {
               //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}